import 'package:car_baidu/core/service/http_config.dart';
import 'package:dio/dio.dart';

class LLHttpRequest {
  static final baseOptions = BaseOptions(
      method: "get",
      baseUrl: HttpConfig.baseUrl,
      connectTimeout: HttpConfig.requestTimeout);

  static Dio dio = Dio(
    baseOptions,
  );
  static Future<Response> httpRequest(String path,
      {String method = "get",
      Map<String, dynamic>? params,
      Map<String, dynamic>? headers,
      InterceptorsWrapper? wrapper}) async {
    // 拦截器
    InterceptorsWrapper baseInterceptor = InterceptorsWrapper(onRequest: (
      RequestOptions options,
      RequestInterceptorHandler handler,
    ) {
      handler.next(options);
    }, onResponse: (
      Response e,
      ResponseInterceptorHandler handler,
    ) {
      handler.next(e);
    }, onError: (
      DioError e,
      ErrorInterceptorHandler handler,
    ) {
      handler.next(e);
    });
    List<InterceptorsWrapper> wrappers = [baseInterceptor];
    if (wrapper != null) {
      wrappers.add(wrapper);
    }
    dio.interceptors.addAll(wrappers);

    Response response = await dio.request(path,
        queryParameters: params,
        options: Options(method: method, headers: headers));
    return response;
  }
}
